package logica;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import presentacion.VentanaMapBuilder;

public class ControladorMouse extends MouseAdapter {

    private VentanaMapBuilder vmb;

    public ControladorMouse(VentanaMapBuilder ventanaMapBuilder) {
        this.vmb = ventanaMapBuilder;
    }

    public void ActivarPropiedadesBloque(MouseEvent me, String condicion) {
        switch (condicion) {
            case "Propiedades": {
                JComponent comp = (JComponent) me.getComponent();
                Component child = comp.findComponentAt(me.getPoint());
                if (child instanceof JLabel) {
                    for (ElementoMapa elemento : vmb.getCm().getElementosMapa()) {
                        if (child == elemento.getLabelImagen()) {
                            Image imagenEscalada = elemento.getImagenBloque().getScaledInstance(vmb.getProps_ImgBloque().getSize().width, vmb.getProps_ImgBloque().getSize().height, Image.SCALE_SMOOTH);
                            vmb.getProps_txtCodigo().setText(String.valueOf(elemento.getBloque().getIdBloque()));
                            vmb.getProps_txtNombre().setText(elemento.getBloque().getNombre());
                            vmb.getProps_txtDesc().setText(elemento.getBloque().getDescripcion());
                            vmb.getProps_txtCateg().setText(elemento.getBloque().getIdCategoria().getNombre());
                            vmb.getProps_ImgBloque().setIcon(new ImageIcon(imagenEscalada));
                            if (vmb.getProps_txtCateg().getText().equalsIgnoreCase("pared")) {
                                vmb.getProps_btnSolido().setSelected(true);
                            } else {
                                vmb.getProps_btnSolido().setSelected(false);
                            }
                        }
                    }
                    vmb.getProps_txtX().setText(String.valueOf(child.getLocation().x) + " (" + child.getLocation().x / 32 + ")");
                    vmb.getProps_txtY().setText(String.valueOf(child.getLocation().y) + " (" + child.getLocation().y / 32 + ")");
                }
                break;
            }
            case "Seleccionar Bloque": {
                List<ElementoMapa> bloqueList = new ArrayList<>();
                Image btnSelectBloque_Icon;
                JComponent comp = (JComponent) me.getComponent();
                Component child = comp.findComponentAt(me.getPoint());
                if (child instanceof JLabel) {
                    for (ElementoMapa elemento : bloqueList) {
                        if (child == elemento.getLabelImagen()) {
                            vmb.getSelecB_txtNombre().setText(elemento.getBloque().getNombre());
                            vmb.getSelecB_txtDesc().setText(elemento.getBloque().getDescripcion());
                            String categoria = elemento.getBloque().getIdCategoria().getNombre();
                            vmb.getSelecB_txtCateg().setText(categoria);
                            btnSelectBloque_Icon = elemento.getImagenBloque().getScaledInstance(vmb.getSelecB_ImgBloque().getSize().width, vmb.getSelecB_ImgBloque().getSize().height, Image.SCALE_SMOOTH);
                            vmb.getSelecB_ImgBloque().setIcon(new ImageIcon(btnSelectBloque_Icon));
                        }
                    }
                }
                break;
            }
        }
    }

    public void ActivarBordeBloque(MouseEvent me, boolean seleccionado) {
        JComponent comp = (JComponent) me.getComponent();
        Component child = comp.findComponentAt(me.getPoint());
        JLabel childLabel = (JLabel) child;
        if (child instanceof JLabel) {
            if (seleccionado == true) {
                childLabel.setBorder(new LineBorder(Color.yellow, 1));
            } else {
                childLabel.setBorder(new LineBorder(Color.white, 1));
            }
        }
    }

    public void DesactivarBordeBloque(MouseEvent me) {
        for (int i = 0; i < vmb.getPanel_editorMapa().getComponentCount(); i++) {
            JLabel childLabel = (JLabel) vmb.getPanel_editorMapa().getComponent(i);
            childLabel.setBorder(null);
        }
    }

    public void PintarBloque(MouseEvent me, ElementoMapa nuevoelemento) {
        JComponent comp = null;
        Component child = null;
        JLabel bloqueMapa = null;
        Image icon_nuevoBloque = null;
        BufferedImage icon_nuevoBloque128 = null;

        try {
            comp = (JComponent) me.getComponent();
            child = comp.findComponentAt(me.getPoint());
            bloqueMapa = (JLabel) child;
            icon_nuevoBloque = nuevoelemento.getImagenBloque().getScaledInstance(32, 32, Image.SCALE_SMOOTH);
            icon_nuevoBloque128 = nuevoelemento.getImagenBloque();
            if (child instanceof JLabel) {
                for (ElementoMapa elemento : vmb.getCm().getElementosMapa()) {
                    if (bloqueMapa == elemento.getLabelImagen()) {
                        elemento.setBloque(nuevoelemento.getBloque());
                        elemento.setImagenBloque(icon_nuevoBloque128);
                        bloqueMapa.setIcon(new ImageIcon(icon_nuevoBloque));
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            comp = null;
            child = null;
            bloqueMapa = null;
            icon_nuevoBloque = null;
            icon_nuevoBloque128 = null;

        }
    }
}
